package com.zprmb.udzt;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.zprmb.udzt.util.Constant;


/**
 * 我的邀请
 * 
 * @author shenshao
 * 
 */
public class My_invate extends BaseActivity {
	private ImageView img_qr;
	private ImageView img_back;
	private Button btn_invate;
	private TextView tv_info;
	private TextView tv_jilu;
	private String ref;
	private RelativeLayout rl_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_invest);
		BbtApplication.getInstance().addActvity(this);
		img_qr = (ImageView) findViewById(R.id.img_my_qr);
		img_back = (ImageView) findViewById(R.id.img_back);
		btn_invate = (Button) findViewById(R.id.btn_invate);
		tv_jilu = (TextView) findViewById(R.id.tv_jilu);
		tv_info = (TextView) findViewById(R.id.tv_info);
		ref = getShare().getString("usercode", "");
		Log.e("ref", ref);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		rl_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		tv_jilu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(My_invate.this, Invatejilu.class);
				startActivity(intent);
			}
		});
		tv_info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(My_invate.this,
						V4WebViewActivity.class);
				intent.putExtra("title", "邀请规则");
				intent.putExtra("url", "/h5/inviteRule?app=true");
				startActivity(intent);
			}
		});
		btn_invate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// shareLogin();
//				initshare();
			}
		});

		img_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		try {

			Bitmap bit = Create2DCode(Constant.URL + "/h5/spreadReg?ref=" + ref);
			img_qr.setImageBitmap(bit);
		} catch (WriterException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	public Bitmap Create2DCode(String str) throws WriterException {
		// 鐢熸垚浜岀淮鐭╅樀,缂栫爜鏃舵寚瀹氬ぇ灏�涓嶈鐢熸垚浜嗗浘鐗囦互鍚庡啀杩涜缂╂斁,杩欐牱浼氭ā绯婂鑷磋瘑鍒け璐�
		BitMatrix matrix = new MultiFormatWriter().encode(str,
				BarcodeFormat.QR_CODE, 300, 300);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		// 浜岀淮鐭╅樀杞负涓�淮鍍忕礌鏁扮粍,涔熷氨鏄竴鐩存í鐫�帓浜�
		int[] pixels = new int[width * height];
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = Color.rgb(29, 165, 246);
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		// 閫氳繃鍍忕礌鏁扮粍鐢熸垚bitmap,鍏蜂綋鍙傝�api
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}

//	private void initshare() {
//		VolleryUtil volleryUtil = new VolleryUtil() {
//
//			@Override
//			public void onUpDate(JSONObject jsonObject) {
//				// TODO Auto-generated method stub
//				Log.e("invate", jsonObject.toString());
//				if (jsonObject.optInt("success") == 1) {
//					JSONObject json = jsonObject.optJSONObject("data");
//					Intent intent = new Intent(My_invate.this,
//							WXEntryActivity.class);
//					if (ref.equals("")) {
//						intent.putExtra("sharelink", json.optString("linkUrl"));
//					} else {
//						intent.putExtra("sharelink", json.optString("linkUrl")
//								+ "?ref=" + ref);
//					}
//					intent.putExtra("description",
//							json.optString("description"));
//					intent.putExtra("title", json.optString("title"));
//					startActivity(intent);
//				} else {
//
//				}
//			}
//
//			@Override
//			public void onError() {
//				// TODO Auto-generated method stub
//
//			}
//		};
//		volleryUtil.setRequestMethod(0);
//		volleryUtil.setShow(true);
//		volleryUtil.execute(My_invate.this, Constant.URL
//				+ "/api/shareProductContent/inviteFriend/inviteFriend", "");
//	}

//	private void shareLogin() {
//
//		Intent intent = new Intent(My_invate.this, WXEntryActivity.class);
//		if (ref.equals("")) {
//			intent.putExtra("sharelink", Constant.URL + "/h5/spreadReg");
//		} else {
//			intent.putExtra("sharelink", Constant.URL + "/h5/spreadReg?ref="
//					+ ref);
//		}
//		intent.putExtra("description", "快去注册！优点智投免费送72000体验金啦！新人投资最高可赚100元收益！");
//		intent.putExtra("title", "我投资了优点智投新人专享福利计划，你也快来看看吧！");
//		startActivity(intent);
//
//	}
}
